<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Http\Requests\ApplicationCouponRequest;
use App\Http\Responses\CustomResponse;
use App\Services\admin\ApplicationCouponService;
use Illuminate\Http\Request;

/**
 * 应用管理 - 优惠券
 *
 * @Desc:
 *
 * @author: zhx
 *
 * @Time: 2023/8/9 14:01
 */
class ApplicationCouponController extends BaseController
{
    /**
     * 显示列表
     *
     * @Desc:
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 14:02
     */
    public function index(Request $request)
    {
        $requestAll = $request->all();
        $list = ApplicationCouponService::getPageData($requestAll);

        return CustomResponse::success($list);
    }

    /**
     * 创建 | 编辑
     *
     * @Desc:
     *
     * @param  ApplicationRequest  $request 验证请求类
     *
     * @author: zhx
     *
     * @Time: 2023/8/10 14:43
     */
    public function store(ApplicationCouponRequest $request)
    {
        if ($request->input('id')) {
            // 编辑
            $requestAll = $request->all(['id', 'name', 'price', 'code', 'type', 'status']);

            $id = ApplicationCouponService::edit($requestAll['id'], $requestAll);

            return $id ? CustomResponse::success([$id], '编辑成功') : CustomResponse::error('编辑失败');

        } else {
            // 创建
            $requestAll = $request->all(['name', 'price', 'code', 'type', 'status']);
            $result = ApplicationCouponService::create($requestAll);

            return $result ? CustomResponse::success([$result->id], '创建成功') : CustomResponse::error('创建失败');
        }
    }

    /**
     * 删除
     *
     * @Desc:
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 14:03
     */
    public function destroy($id)
    {
        $ids = explode(',', $id);
        $result = ApplicationCouponService::destroy($ids);

        return $result ? CustomResponse::success([$result->id], '删除成功') : CustomResponse::error('删除失败');
    }
}
